<script setup>
import { onLoad } from '@dcloudio/uni-app';
import { getDicts } from '@/api/common';
import { appStore } from '@/sotre/modules/app';

const { userInfo } = toRefs(appStore());

const $post = inject('$post');
const $get = inject('$get');

const companyForm = reactive({
    enterpriseName: '',
    industry: '',
    scale: '',
    position: '',
    demand: '',
    contactPhone: '',
    contact: '',
    agree: ['true'],
    type: '0',
});
const companyRef = ref();
const scale = ref([]);
const position = ref([]);

onLoad(options => {
    companyForm.type = options.type;
});

getDicts('enterprise_scale').then(res => {
    scale.value = res.data;
});
getDicts('position_type').then(res => {
    position.value = res.data;
});

function submit() {
    companyRef.value.validate().then(({ valid }) => {
        if (valid) {
            uni.showLoading({
                title: '提交中...',
                mask: true,
            });
            $post(
                '/auth/platform/enterpriseAuth',
                companyForm,
                uni.getStorageSync('TEMP_TOKEN')
            ).then(res => {
                uni.hideLoading();
                if (res.code == 200) {
                    $get('/platform/users/getUser', {}, uni.getStorageSync('TEMP_TOKEN')).then(
                        r => {
                            console.log(r);

                            const token = uni.getStorageSync('TEMP_TOKEN');
                            uni.setStorageSync('TOKEN', token);
                            uni.setStorageSync(
                                'enterpriseId',
                                r.data.platformUsers.lastEnterpriseId
                            );
                            userInfo.value = r.data.platformUsers;
                            setTimeout(() => {
                                uni.switchTab({
                                    url: '/pages/home/index',
                                });
                            }, 500);
                        }
                    );
                    // if (companyForm.type == 0) {
                    //     uni.showToast({
                    //         title: '企业认证信息提交成功',
                    //         icon: 'success',
                    //     });
                    //     // setTimeout(() => {
                    //     //     userInfo.value.state = 3;
                    //     //     uni.navigateBack();
                    //     // }, 1000);
                    // } else {
                    //     const token = uni.getStorageSync('TEMP_TOKEN');
                    //
                    // }
                }
            });
        }
    });
}
</script>

<template>
    <div class="company_box">
        <u-navbar title="企业认证" :autoBack="true" placeholder></u-navbar>
        <div class="box">
            <div class="company">
                <wd-form :model="companyForm" ref="companyRef">
                    <wd-input
                        type="text"
                        v-model="companyForm.enterpriseName"
                        placeholder="请输入企业名称"
                        size="large"
                        prop="enterpriseName"
                        label="企业名称"
                        labelWidth="80px"
                        :rules="[{ required: true, message: '请输入企业名称' }]"
                    />
                    <wd-input
                        type="text"
                        v-model="companyForm.industry"
                        placeholder="所属行业"
                        size="large"
                        prop="industry"
                        label="所属行业"
                        labelWidth="80px"
                        :rules="[{ required: true, message: '请输入所属行业' }]"
                    />
                    <wd-picker
                        size="large"
                        :columns="scale"
                        labelWidth="80px"
                        value-key="dictValue"
                        label-key="dictLabel"
                        label="企业规模"
                        v-model="companyForm.scale"
                        :rules="[{ required: true, message: '请选择企业规模' }]"
                    />
                    <wd-picker
                        size="large"
                        :columns="position"
                        labelWidth="80px"
                        label="您的职业"
                        value-key="dictValue"
                        label-key="dictLabel"
                        prop="position"
                        v-model="companyForm.position"
                        :rules="[{ required: true, message: '请选择您的职业' }]"
                    />
                    <wd-input
                        type="text"
                        v-model="companyForm.demand"
                        placeholder="您的需求"
                        size="large"
                        prop="demand"
                        label="您的需求"
                        labelWidth="80px"
                        :rules="[{ required: true, message: '请输入您的需求' }]"
                    />
                    <wd-input
                        type="text"
                        v-model="companyForm.contact"
                        placeholder="联系人"
                        size="large"
                        prop="contact"
                        label="联系人"
                        labelWidth="80px"
                        :rules="[{ required: true, message: '请输入联系人' }]"
                    />
                    <wd-input
                        type="text"
                        v-model="companyForm.contactPhone"
                        placeholder="联系电话"
                        size="large"
                        prop="contactPhone"
                        label="联系电话"
                        labelWidth="80px"
                        :rules="[{ required: true, message: '请输入联系电话' }]"
                    />
                    <u-button type="warning" size="large" @click="submit">提交</u-button>
                </wd-form>
            </div>
        </div>
        <div class="tip">
            <u-checkbox-group v-model="companyForm.agree">
                <u-checkbox
                    shape="circle"
                    activeColor="#fb6f2e"
                    name="true"
                    label="以阅读并同意轨迹九尾狐"
                ></u-checkbox>
            </u-checkbox-group>
            <span style="color: #fb6f2e">《用户协议》</span>
            和
            <span style="color: #fb6f2e">《隐私协议》</span>
        </div>
    </div>
</template>

<style lang="scss" scoped>
.company_box {
    display: flex;
    flex-direction: column;
    height: 100vh;
    .tip {
        font-size: 13px;
        color: #999;
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-top: auto;
        margin-bottom: 20px;
        :deep(.u-checkbox) {
            margin: 0;
            text {
                font-size: 13px !important;
            }
        }
    }
    .box {
        .center {
            text-align: center;
        }
        .company {
            padding: 0 15px;
            :deep(.wd-form) {
                display: flex;
                flex-direction: column;
                padding: 50px 10px;
                gap: 15px;
                .wd-input {
                    border: 1px solid #e0e0e0;
                    border-radius: 4px;
                    box-sizing: border-box;
                    transition: all 0.2s;
                    background: none;
                }
                .wd-picker {
                    .wd-picker__cell {
                        background: none;
                        border: 1px solid #e0e0e0;
                        border-radius: 4px;
                        box-sizing: border-box;
                        transition: all 0.2s;
                        padding: 12px;
                    }
                }
            }
        }
    }
}
</style>
